[LIBXC] Make strerror() thread-safe by protecting it with a mutex.
authorkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Fri, 8 Dec 2006 15:59:30 +0000 (15:59 +0000)
committerkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Fri, 8 Dec 2006 15:59:30 +0000 (15:59 +0000)
Using strerror_r() is made difficult by the different GNU definition,
and different distros variously choose the POSIX or GNU prototype.
Signed-off-by: Keir Fraser <keir@xensource.com>
tools/libxc/Makefile
tools/libxc/xc_private.c

index 129b867ff6783d5bdb6792b0e467c91795299363..f92cde836d0ea68f0ed544574d412ff146b5c782 100644 (file)
@@ -119,7 +119,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
        ln -sf $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^
+       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^ -lpthread
 
 # libxenguest
 
@@ -132,7 +132,7 @@ libxenguest.so.$(MAJOR): libxenguest.so.$(MAJOR).$(MINOR)
        ln -sf $< $@
 
 libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
-       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl
+       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl -lpthread
 
 -include $(DEPS)
 
index ed76cf003c2887b88dfe540e28104ae18639389d..82e190989ce647f3927d7db1435ed8be6946b9c4 100644 (file)
@@ -7,8 +7,8 @@
 #include <inttypes.h>
 #include "xc_private.h"
 #include "xg_private.h"
-
 #include <stdarg.h>
+#include <pthread.h>
 
 static __thread xc_error last_error = { XC_ERROR_NONE, ""};
 #if DEBUG
@@ -486,14 +486,20 @@ unsigned long xc_make_page_below_4G(
 char *safe_strerror(int errcode)
 {
     static __thread char errbuf[32];
-#ifdef __GLIBC__
-    /* Broken GNU definition of strerror_r may not use our supplied buffer. */
-    return strerror_r(errcode, errbuf, sizeof(errbuf));
-#else
-    /* Assume we have the POSIX definition of strerror_r. */
-    strerror_r(errcode, errbuf, sizeof(errbuf));
+    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+    char *strerror_str;
+
+    /*
+     * Thread-unsafe strerror() is protected by a local mutex. We copy
+     * the string to a thread-private buffer before releasing the mutex.
+     */
+    pthread_mutex_lock(&mutex);
+    strerror_str = strerror(errcode);
+    strncpy(errbuf, strerror_str, sizeof(errbuf));
+    errbuf[sizeof(errbuf)-1] = '\0';
+    pthread_mutex_unlock(&mutex);
+
     return errbuf;
-#endif
 }
 
 /*